#include "Patrol.h"
#include "Attack.h"

Patrol* Patrol::pInst=NULL;


Patrol::Patrol()
{

}

Patrol::~Patrol()
{

}

Patrol* Patrol::GetInstance()
{
	if(pInst == NULL)
	{
		pInst = new Patrol();
	}

	return pInst;

}

void Patrol::Enter(AbstractEnemy* pEnemy)
{
	//set pathfinding on
	pEnemy->GetBehaviours()->SetPathFindOn(true);

	//set target to colest point in patrol
	pEnemy->GetBehaviours()->SetTargetClosestPatrolPoint();
}

void Patrol::Execute(AbstractEnemy* pEnemy)
{
	//get new velocities from behaviors
	XMFLOAT3 velocity1 = pEnemy->GetVelocity();
	XMFLOAT3 velocity2 = pEnemy->GetBehaviours()->AccumulateBehaviours();

	//add velocities 
	velocity1.x += velocity2.x;
	velocity1.z += velocity2.z;

	//set new velocity
	pEnemy->SetVelocity(velocity1);

	//check player agaisnt aggro radius. if player is wihin radius chnage state
	if((pEnemy->GetBehaviours()->GetDistance(pEnemy->GetPlayerPos(),pEnemy->GetPosition())) < pEnemy->GetAggroRadius())
	{
		pEnemy->ChangeState(Attack::GetInstance());
	}

}

void Patrol::Exit(AbstractEnemy* pEnemy)
{
	//turn pathfinding off
	pEnemy->GetBehaviours()->SetPathFindOn(false);
}

void Patrol::Release()		// Static
{

	if(pInst)
	{
		delete pInst;
		pInst = NULL;
	}
}

